﻿using System.Windows;
using System.Windows.Media;
using C1.Silverlight.Pdf;
using System;

namespace MyFloridaHouse.libSvOpenDistricts.PDF
{
    public class PDFString
    {
        C1PdfDocument pdf;

        public string text { get; set; }
        string fontName;
        public double fontSize {get; set;}
        PdfFontStyle fontStyle;
        Color color;
        double padding;

        public Font font { get; set; }
        public Size textBoxSize { get; set; } 

        public PDFString(C1PdfDocument pdf, string fontName, double fontSize, PdfFontStyle fontStyle, Color color, string text, double padding)
        {
            this.pdf = pdf;
            this.fontName = fontName;
            this.fontSize = fontSize;
            this.fontStyle = fontStyle;
            this.color = color;
            this.text = text;
            this.padding = padding;

            UpdateFontSize(fontSize);
        }

        public void UpdateFontSize(double fontSize)
        {
            this.fontSize = fontSize;
            font = new Font(fontName, fontSize, fontStyle);
            textBoxSize = pdf.MeasureString(text, font);
        }

        public void Draw(Rect rect)
        {
            pdf.DrawString(text, font, color, rect);
        }

        public void Draw(double x, double y, double width, double height)
        {
            pdf.DrawString(text, font, color, new Rect(x,y,width,height));
        }

        public void DrawCenteredX(Rect rect)
        {
            Rect textRect = new Rect(rect.X, rect.Y, rect.Width, rect.Height);
            textRect.X += padding + (rect.Width - textBoxSize.Width) / 2;

            // adjust for incorrect way C1PDF calculates height
            double kludgeFactor = .15;
            textRect.Y += -textBoxSize.Height * kludgeFactor;
            
            pdf.DrawString(text, font, color, textRect);

        }

        public void DrawCenteredX(PDFPoint cursor, Rect clipRect)
        {
            PDFPoint textCursor = new PDFPoint(cursor);
            textCursor.AddX(-textBoxSize.Width/2);

            // adjust for incorrect way C1PDF calculates height
            Rect textRect = new Rect(textCursor.x, textCursor.y, textBoxSize.Width, textBoxSize.Height);
            double kludgeFactor = .1;
            textRect.Y += -textBoxSize.Height * kludgeFactor;
            pdf.SetClipRect(clipRect);
            pdf.DrawString(text, font, color, textRect);
            pdf.ResetClipRect();
        }

        public bool DrawInRectangle(PDFPoint cursor, Rect clipRect)
        {
            PDFPoint textCursor = new PDFPoint(cursor);

            if (((clipRect.Left <= textCursor.x) & (textCursor.x <= clipRect.Right))
                & ((clipRect.Bottom >= textCursor.y) & (textCursor.y >= clipRect.Top)))
            {
                DrawCenteredX(textCursor, clipRect);
                return true;
            }
            else
            {
                return false;
            }

        }

        public void DrawLeftAbove(PDFPoint cursor)
        {
            PDFPoint textCursor = new PDFPoint(cursor);
            textCursor.AddY(- textBoxSize.Height);


            // adjust for incorrect way C1PDF calculates height
            double kludgeFactor = .4;
            textCursor.AddY(-textBoxSize.Height * kludgeFactor);

            Rect rect = new Rect(textCursor.x, textCursor.y, textBoxSize.Width, textBoxSize.Height);
            pdf.DrawString(text, font, color, rect);

        }


        public void FitFontSize(double boundaryWidth, double boundaryHeight)
        {
            // loop, increasing the size of the text until it doesn't fit in the box anymore
            double textFontSize = 1;
            bool increaseTextSize = true;
            while (increaseTextSize)
            {
                UpdateFontSize(++textFontSize);

                double requiredHeight = padding + textBoxSize.Height + padding;
                double requiredWidth = padding + textBoxSize.Width + padding;  

                if ((requiredWidth >= boundaryWidth) | (requiredHeight >= boundaryHeight))
                {
                    increaseTextSize = false;
                }
            }
            textFontSize--;

            UpdateFontSize(textFontSize);
        }

    }
}
